//
//  DJMYBIVC.m
//  shortvideoplay
//
//  Created by tikeios_002 on 2024/7/9.
//

#import "DJMYBIVC.h"
#import "DJMYBICell.h"
#import "DJMYBICell.h"
#import "AgreementViewController.h"
#import "PQIAPManager.h"

@interface DJMYBIVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *biLabel;
@property (weak, nonatomic) IBOutlet UIButton *agreenButton;
@property (weak, nonatomic) IBOutlet UIButton *chongzhiButton;
@property (weak, nonatomic) IBOutlet UICollectionView *collection;
@property (assign, nonatomic) CGFloat itemW;
@property (strong, nonatomic)NSArray * listArr;
@property (strong, nonatomic)NSString * seleteRow;
@end

@implementation DJMYBIVC

-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
    
}
- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationItem.title = @"钻石充值";
    self.itemW = (KScreen_W-50)/3-0.1;
    [self.collection registerNib:[UINib nibWithNibName:@"DJMYBICell" bundle:nil] forCellWithReuseIdentifier:@"DJMYBICell"];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    self.chongzhiButton.backgroundColor = MainColor;
    self.chongzhiButton.layer.cornerRadius = 8;
    [self requestBi];
}
// 请求我的视频
- (void)requestBi{
    KWeakSelf
    KMutableDic(sendDic)
    [YBNetBase translateCoinPriceRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            weakSelf.listArr = data[@"list"];
            [weakSelf.collection reloadData];
            weakSelf.biLabel.text = [NSString stringWithFormat:@"%@",data[@"translate_coin"]];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}

- (IBAction)agreenTap:(id)sender {
    self.agreenButton.selected = !self.agreenButton.selected;
}
- (IBAction)xieyiTap:(id)sender {
    AgreementViewController * xy = [[AgreementViewController alloc]init];
    xy.modalPresentationStyle = UIModalPresentationFullScreen;
    xy.url = [QZUserInfoModel share].cha;
    xy.Nav_title = @"用户充值协议";
    xy.isPerSent = YES;
    [self presentViewController:xy animated:YES completion:nil];
}
- (IBAction)chongzhiTap:(id)sender {
    if(!self.agreenButton.selected){
        [YBMsgPop showPop:@"阅读并同意协议"];
        return;
    }
    if(self.seleteRow.length == 0){
        [YBMsgPop showPop:@"请选择充值金额"];
        return;
    }
    KWeakSelf
    KMutableDic(sendDic)
    int index = [self.seleteRow intValue];
    sendDic[@"vipid"] = self.listArr[index][@"id"];
    //获取订单
    [SVProgressHUDTool showWithStatus:nil];
    
    [YBNetBase orderCreateTBeanRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        if(code == 200){
            [weakSelf startAppleBuyID:data[@"order_no"]];
        }else{
            [YBMsgPop showPop:msg];
        }
    }];
}
- (IBAction)backTap:(id)sender {
    if(self.isPre){
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)startAppleBuyID:(id)orderId{
    KWeakSelf
    int index = [self.seleteRow intValue];
    dispatch_async(dispatch_get_main_queue(), ^{
        // 需要在主线程执行的代码
        [[PQIAPManager sharedManager] requestProductWithId:self.listArr[index][@"productid"] orderId:orderId type:2 buySuccess:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                // 需要在主线程执行的代码
                weakSelf.seleteRow = @"";
                [weakSelf requestBi];
                [YBMsgPop showPop:@"恭喜你，充值成功。"];
            });
        } buyFailuer:^{
            dispatch_async(dispatch_get_main_queue(), ^{
                // 需要在主线程执行的代码
                [SVProgressHUDTool dismiss];
            });
        } checkFailuer:^(NSString *str) {
            dispatch_async(dispatch_get_main_queue(), ^{
                // 需要在主线程执行的代码
                [SVProgressHUDTool dismiss];
                [YBMsgPop showPop:str];
            });
        }];
    });
}
#pragma mark - UICollectionView data source
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section;
{
    return self.listArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    DJMYBICell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DJMYBICell" forIndexPath:indexPath];
    NSDictionary *datas = self.listArr[indexPath.row];
    cell.biLabel.text = [NSString stringWithFormat:@"%@",datas[@"coin_base"]];
    cell.moneyLabel.text = datas[@"titles"];
    cell.songLabel.text = [NSString stringWithFormat:@"赠%@",datas[@"give_coin"]];
    
    if(self.seleteRow.length > 0 && indexPath.row == [self.seleteRow intValue]){
        //拉伸后的图片
        cell.layer.borderWidth = 2;
    }else{
        cell.layer.borderWidth = 0;
    }
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.itemW, 85);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
                        layout:(UICollectionViewLayout *)collectionViewLayout
        insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(20, 15, 0, 15);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    return CGSizeZero;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10;
}
-(CGFloat)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section

{
    return 10;

}
- (void)showTextViewSubViewToViewTo1:(UIView * )subView{

    UIView * showTextViewSubViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewSubViewTo1.clipsToBounds = YES;
    [subView addSubview:showTextViewSubViewTo1];
    UILabel * showTextViewSubViewLabelTo2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    showTextViewSubViewLabelTo2.text = @"56464645";
    [showTextViewSubViewTo1 addSubview:showTextViewSubViewLabelTo2];
    
    UIImageView * showTextViewSubViewTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [subView addSubview:showTextViewSubViewTo2];

}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.seleteRow = [NSString stringWithFormat:@"%ld",indexPath.row];
    [collectionView reloadData];
}
-(void)showTextViewAddShowViewTo2{
    UIView * showTextViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 100, 360)];
    showTextViewViewTo1.backgroundColor = [UIColor whiteColor];
    showTextViewViewTo1.layer.cornerRadius = 8;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:showTextViewViewTo1];
    
    UIImageView * showTextViewimgTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(K_AUTOLAYOUT_W(157.5), K_AUTOLAYOUT_W(260), K_AUTOLAYOUT_W(60), K_AUTOLAYOUT_W(60))];
    showTextViewimgTo2.layer.cornerRadius = K_AUTOLAYOUT_W(30);
    showTextViewimgTo2.clipsToBounds = YES;
    showTextViewimgTo2.contentMode = UIViewContentModeScaleAspectFill;
    showTextViewimgTo2.hidden = YES;
    [showTextViewViewTo1 addSubview:showTextViewimgTo2];
    
    UILabel *  showTextViewimgTo3 = [[UILabel alloc]initWithFrame:CGRectMake(K_AUTOLAYOUT_W(15), K_AUTOLAYOUT_W(35), K_AUTOLAYOUT_W(345), K_AUTOLAYOUT_W(25))];
    showTextViewimgTo3.font = FontBold(18);
    showTextViewimgTo3.textColor = ThreeColor;
    showTextViewimgTo3.textAlignment = NSTextAlignmentCenter;
    showTextViewimgTo3.hidden = YES;
    [showTextViewViewTo1 addSubview:showTextViewimgTo3];
}
- (NSString *)showTextViewGetNowTimerTo3{
    NSDateFormatter *showTextViewGetNowFa = [[NSDateFormatter alloc] init];
    [showTextViewGetNowFa setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    return [showTextViewGetNowFa stringFromDate:[NSDate date]];
}
@end
